import { ArrayCopy } from "./array-utils";

export default class ArrayCache<T> {

	protected _list: T[];

	protected _index: number;

	protected cache_size: number;

	public get list() {
		return this._list;
	}

	public get empty() {
		return this.length === 0;
	}

	public get length() {
		return this._index;
	}

	public constructor(cache_size: number) {
		this.cache_size = cache_size;
		this._index = 0;
		this._list = [];
	}

	public push(...values: T[]): void {
		this._list = ArrayCopy(this._list, this._index + this.cache_size);
		for (const value of values) {
			this._list[this._index++] = value;
		}
	}

	public copy(): ArrayCache<T> {
		const newArr = new ArrayCache<T>(this.cache_size);
		newArr._index = this._index;
		newArr._list = ArrayCopy(this._list);
		return newArr;
	}
}
